package com.ruoyi.cmemetal.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.cmecomon.mapper.ZxOrderPostmanMapper;
import com.ruoyi.cmemetal.domain.*;
import com.ruoyi.cmemetal.mapper.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cmemetal.service.IZxOrderFormService;
import org.springframework.transaction.annotation.Transactional;

/**
 * orderService业务层处理
 *
 * @author zhangxu
 * @date 2021-12-08
 */
@Service
public class ZxOrderFormServiceImpl implements IZxOrderFormService
{
    @Autowired
    private ZxOrderFormMapper zxOrderFormMapper;

    @Autowired
    private ZxDataPreprocessMapper zxDataPreprocessMapper;

    @Autowired
    private ZxOrderUrlMapper zxOrderUrlMapper;

    @Autowired
    private ZxPrintProcessMapper zxPrintProcessMapper;

    @Autowired
    private ZxHeatPreprocessMapper zxHeatPreprocessMapper;

    @Autowired
    private ZxWireCutMapper zxWireCutMapper;

    @Autowired
    private ZxPostProcessMapper zxPostProcessMapper;

    @Autowired
    private ZxOrderPostmanMapper zxOrderPostmanMapper;










    /**
     * 查询order
     *
     * @param id order主键
     * @return order
     */
    @Override
    public ZxOrderForm selectZxOrderFormById(Long id)
    {
        return zxOrderFormMapper.selectZxOrderFormById(id);
    }

    /**
     * 查询order列表
     *
     * @param zxOrderForm order
     * @return order
     */
    @Override
    public List<ZxOrderForm> selectZxOrderFormList(ZxOrderForm zxOrderForm)
    {
        return zxOrderFormMapper.selectZxOrderFormList(zxOrderForm);
    }

    /**
     * 新增order
     *
     * @param zxOrderForm order
     * @return 结果
     */
    @Override
    @Transactional
    public int insertZxOrderForm(ZxOrderForm zxOrderForm)
    {
        //新增预处理数据
        ZxDataPreprocess zxDataPreprocess = new ZxDataPreprocess();
        zxDataPreprocess.setOrderId(zxOrderForm.getOrderId());
        zxDataPreprocessMapper.insertZxDataPreprocess(zxDataPreprocess);

        //新增相关路径
        ZxOrderUrl zxOrderUrl = new ZxOrderUrl();
        zxOrderUrl.setOederid(zxOrderForm.getOrderId());
        zxOrderUrlMapper.insertZxOrderUrl(zxOrderUrl);

        //新增打印处理数据
        ZxPrintProcess zxPrintProcess = new ZxPrintProcess();
        zxPrintProcess.setOrderId(zxOrderForm.getOrderId());
        zxPrintProcessMapper.insertZxPrintProcess(zxPrintProcess);

        //新增热处理数据
        ZxHeatPreprocess zxHeatPreprocess = new ZxHeatPreprocess();
        zxHeatPreprocess.setOrderId(zxOrderForm.getOrderId());
        zxHeatPreprocessMapper.insertZxHeatPreprocess(zxHeatPreprocess);

        //新增线切割数据
        ZxWireCut zxWireCut = new ZxWireCut();
        zxWireCut.setOrderId(zxOrderForm.getOrderId());
        zxWireCutMapper.insertZxWireCut(zxWireCut);

        //新增后处理数据
        ZxPostProcess zxPostProcess = new ZxPostProcess();
        zxPostProcess.setOrderId(zxOrderForm.getOrderId());
        zxPostProcessMapper.insertZxPostProcess(zxPostProcess);

        return zxOrderFormMapper.insertZxOrderForm(zxOrderForm);
    }

    /**
     * 修改order
     *
     * @param zxOrderForm order
     * @return 结果
     */
    @Override
    public int updateZxOrderForm(ZxOrderForm zxOrderForm)
    {


        return zxOrderFormMapper.updateZxOrderForm(zxOrderForm);
    }

    /**
     * 批量删除order
     *
     * @param ids 需要删除的order主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteZxOrderFormByIds(Long[] ids)
    {

        ZxOrderForm zxOrderForm = null;

        ZxDataPreprocess zxDataPreprocess = null;
        List<ZxDataPreprocess> zxDataPreprocesses = null;

        ZxOrderUrl zxOrderUrl = null;
        List<ZxOrderUrl> zxOrderUrls = null;

        ZxPrintProcess zxPrintProcess = null;
        List<ZxPrintProcess> zxPrintProcesses = null;

        ZxHeatPreprocess zxHeatPreprocess = null;
        List<ZxHeatPreprocess> zxHeatPreprocesses = null;

        ZxWireCut zxWireCut = null;
        List<ZxWireCut> zxWireCuts = null;

        ZxPostProcess zxPostProcess = null;
        List<ZxPostProcess> zxPostProcesses = null;

        int i;

        if (ids.length > 0){
            for (Long id : ids) {
                i = 0;
                zxOrderForm = zxOrderFormMapper.selectZxOrderFormById(id);

                //删除预处理相关数据
                zxDataPreprocess = new ZxDataPreprocess();
                zxDataPreprocess.setOrderId(zxOrderForm.getOrderId());
                 zxDataPreprocesses= zxDataPreprocessMapper.selectZxDataPreprocessList(zxDataPreprocess);
                zxDataPreprocessMapper.deleteZxDataPreprocessById(zxDataPreprocesses.get(0).getId());

                //删除预处理人
                zxOrderPostmanMapper.deleteZxOrderPostmanByOrderId(zxOrderForm.getOrderId());

                //删除url相关数据
                zxOrderUrl = new ZxOrderUrl();
                zxOrderUrl.setOederid(zxOrderForm.getOrderId());
                 zxOrderUrls = zxOrderUrlMapper.selectZxOrderUrlList(zxOrderUrl);
                 zxOrderUrlMapper.deleteZxOrderUrlById(zxOrderUrls.get(0).getId());

                //删除打印处理数据
                zxPrintProcess = new ZxPrintProcess();
                zxPrintProcess.setOrderId(zxOrderForm.getOrderId());
                zxPrintProcesses = zxPrintProcessMapper.selectZxPrintProcessList(zxPrintProcess);
                zxPrintProcessMapper.deleteZxPrintProcessById(zxPrintProcesses.get(0).getId());

                //删除热处理数据
                zxHeatPreprocess = new ZxHeatPreprocess();
                zxHeatPreprocess.setOrderId(zxOrderForm.getOrderId());
                zxHeatPreprocesses = zxHeatPreprocessMapper.selectZxHeatPreprocessList(zxHeatPreprocess);
                zxHeatPreprocessMapper.deleteZxHeatPreprocessById(zxHeatPreprocesses.get(0).getId());

                //删除线切割数据
                zxWireCut = new ZxWireCut();
                zxWireCut.setOrderId(zxOrderForm.getOrderId());
                zxWireCuts = zxWireCutMapper.selectZxWireCutList(zxWireCut);
                zxWireCutMapper.deleteZxWireCutById(zxWireCuts.get(0).getId());

                //删除后处理数据
                zxPostProcess = new ZxPostProcess();
                zxPostProcess.setOrderId(zxOrderForm.getOrderId());
                zxPostProcesses = zxPostProcessMapper.selectZxPostProcessList(zxPostProcess);
                zxPostProcessMapper.deleteZxPostProcessById(zxPostProcesses.get(0).getId());

                i++;

            }
        }


        return zxOrderFormMapper.deleteZxOrderFormByIds(ids);
    }

    /**
     * 删除order信息
     *
     * @param id order主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteZxOrderFormById(Long id)
    {
        ZxOrderForm zxOrderForm = zxOrderFormMapper.selectZxOrderFormById(id);


        //删除相关预处理数据
        ZxDataPreprocess zxDataPreprocess =new ZxDataPreprocess();
        zxDataPreprocess.setOrderId(zxOrderForm.getOrderId());
        List<ZxDataPreprocess> zxDataPreprocesses = zxDataPreprocessMapper.selectZxDataPreprocessList(zxDataPreprocess);
        zxDataPreprocessMapper.deleteZxDataPreprocessById(zxDataPreprocesses.get(0).getId());

        //删除相关url数据
        ZxOrderUrl zxOrderUrl = new ZxOrderUrl();
        zxOrderUrl.setOederid(zxOrderForm.getOrderId());
        List<ZxOrderUrl> zxOrderUrls = zxOrderUrlMapper.selectZxOrderUrlList(zxOrderUrl);
        zxOrderUrlMapper.deleteZxOrderUrlById(zxOrderUrls.get(0).getId());

        //删除打印处理数据
        ZxPrintProcess zxPrintProcess = new ZxPrintProcess();
        zxPrintProcess.setOrderId(zxOrderForm.getOrderId());
        List<ZxPrintProcess> zxPrintProcesses = zxPrintProcessMapper.selectZxPrintProcessList(zxPrintProcess);
        zxPrintProcessMapper.deleteZxPrintProcessById(zxPrintProcesses.get(0).getId());

        //删除热处理数据
        ZxHeatPreprocess zxHeatPreprocess = new ZxHeatPreprocess();
        zxHeatPreprocess.setOrderId(zxOrderForm.getOrderId());
        List<ZxHeatPreprocess> zxHeatPreprocesses = zxHeatPreprocessMapper.selectZxHeatPreprocessList(zxHeatPreprocess);
        zxHeatPreprocessMapper.deleteZxHeatPreprocessById(zxHeatPreprocesses.get(0).getId());

        //删除线切割数据
        ZxWireCut zxWireCut = new ZxWireCut();
        zxWireCut.setOrderId(zxOrderForm.getOrderId());
        List<ZxWireCut> zxWireCuts = zxWireCutMapper.selectZxWireCutList(zxWireCut);
        zxWireCutMapper.deleteZxWireCutById(zxWireCuts.get(0).getId());

        //删除后处理数据
        ZxPostProcess zxPostProcess = new ZxPostProcess();
        zxPostProcess.setOrderId(zxOrderForm.getOrderId());
        List<ZxPostProcess> zxPostProcesses = zxPostProcessMapper.selectZxPostProcessList(zxPostProcess);
        zxWireCutMapper.deleteZxWireCutById(zxPostProcesses.get(0).getId());

        return zxOrderFormMapper.deleteZxOrderFormById(id);
    }
    /**
     * 根据时间查询当日order
     *
     * @param begainDate 查询日期
     * @return 结果
     */
    @Override
    public List<ZxOrderForm> selectZxOrderBydate(Date begainDate) {
        return zxOrderFormMapper.selectZxOrderBydate(begainDate);
    }




}
